package com.tianhui.consignortwofei.widget;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.mvp.model.enty.UserInfo;
import com.tianhui.consignortwofei.mvp.ui.activity.auth.ConsignorAuthActivity;
import com.tianhui.consignortwofei.role.RoleController;


/**
 * @author fgs
 * @date 2021/3/2
 * @description
 **/
public class AuthStatusView extends LinearLayout {
    protected LinearLayout linearLayout;
    protected TextView contentTextView;
    protected TextView checkTextView;
    private UserInfo userInfo;

    public AuthStatusView(Context context) {
        this(context, null);
    }

    public AuthStatusView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AuthStatusView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_auth_status, null);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        addView(view);

        linearLayout = view.findViewById(R.id.layout_auth_status_authStatusLinearLayout);
        contentTextView = view.findViewById(R.id.layout_auth_status_view_contentTextView);
        checkTextView = view.findViewById(R.id.layout_auth_status_checkTextView);

        checkTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAllInfo();
            }
        });
    }

    public void showAuthStatus(UserInfo userInfo) {

        boolean isShowAuthStatus = RoleController.getInstance().isShowPerfectData();
        if (!isShowAuthStatus) {
            return;
        }

        String isAudit = "0";
        this.userInfo = userInfo;

        if (userInfo != null && userInfo.profile != null) {
            isAudit = userInfo.profile.roleinfoisaudit;
        }
        if (TextUtils.isEmpty(isAudit)) {
            isAudit = "0";
        }

        String tip = getContext().getString(R.string.tip_can_not_delver_goods);

        if (isAudit.equals("1")) { //货主已认证
            linearLayout.setVisibility(View.GONE);
        } else if (isAudit.equals("2")) {//货主审核中
            tip = "资料审核中，您还无法发单......";
            contentTextView.setTextColor(getResources().getColor(R.color.colorOrange));
            contentTextView.setText(tip);
            linearLayout.setVisibility(View.VISIBLE);
        } else {
            contentTextView.setText(tip);
            linearLayout.setVisibility(View.VISIBLE);
        }
    }


    private void checkAllInfo() {
        if (userInfo != null) {
            openActivity(ConsignorAuthActivity.class);
        }

    }

    public void openActivity(Class<?> pClass, Bundle bundle) {
        Intent intent = new Intent(getContext(), pClass);
        intent.putExtras(bundle);
        getContext().startActivity(intent);
    }

    public void openActivity(Class<?> pClass) {
        Intent intent = new Intent(getContext(), pClass);
        getContext().startActivity(intent);
    }
}
